package org.imaginedays.androidall.appwidget;

import org.imaginedays.androidall.R;
import org.imaginedays.androidall.app.AndroidAllApp;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * AppWidget
 * 
 * @author imaginedays
 * 
 */

public class AppWidgetApn extends AppWidgetProvider {

	// 定义 widget接收事件的action
	public static final String ACTION_CLICK = "org.imaginedays.androidall.widget";

	// @Override api 16
	// public void onAppWidgetOptionsChanged(Context context,
	// AppWidgetManager appWidgetManager, int appWidgetId,
	// Bundle newOptions) {
	// super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,
	// newOptions);
	// }

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.i(AndroidAllApp.TAG, "onDeleted");
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Log.i(AndroidAllApp.TAG, "onDisabled");
	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.i(AndroidAllApp.TAG, "onEnabled");
	}

	@Override
	/**
	 * 界面接收事件
	 */
	public void onReceive(Context context, Intent intent) {

		Log.i(AndroidAllApp.TAG, "onReceive");

		final String action = intent.getAction();

		if (action.equals(Intent.ACTION_TIME_TICK)) {

		} else if (action.equals(ACTION_CLICK)) {
              toggleApnStatus(context);
              updateWidget(context);
		} else {
			super.onReceive(context, intent);
		}

	}

	

	@Override
	/**
	 * 界面更新
	 */
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Log.i(AndroidAllApp.TAG, "onUpdate");
		// 每次更新widget的时候 让界面，或者按钮接受点击事件。
		updateWidget(context);
	}

	private void updateWidget(Context context) {
		// 构建一个remoteView 代表桌面上的view
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.apnwidget);
		ComponentName provider = new ComponentName(context, AppWidgetApn.class);
		AppWidgetManager appWidgetMgr = AppWidgetManager.getInstance(context);
		remoteView.setImageViewResource(R.id.iv_widget,
				getApnStatus(context) ? R.drawable.apn_on : R.drawable.apn_off);
		setClickable(context, remoteView);
		appWidgetMgr.updateAppWidget(provider, remoteView);
		//更新多个ids
//		appWidgetMgr.updateAppWidget(appWidgetIds, views)
	}

	/**
	 * 
	 * @param context
	 * @param remoteView
	 */
	private void setClickable(Context context, RemoteViews remoteView) {
		// TODO 如何给widget设置点击事件
		final Intent intent = new Intent(ACTION_CLICK);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intent, 0);
		remoteView
				.setOnClickPendingIntent(R.id.rl_widget_layout, pendingIntent);

	}
	
	private void toggleApnStatus(Context context) {
		setApnStatus(context,getApnStatus(context)?false:true);
		
	}

	private void setApnStatus(Context context, boolean on) {
		if (isICS()) {
			 (new ApnICS()).setApnStatus(context,on);
		}else{
			 (new ApnLegacy()).setApnStatus(context,on);
		}
	}

	private boolean getApnStatus(Context context) {
		if (isICS()) {
			return (new ApnICS()).getApnStatus(context);
		} else {
			return (new ApnLegacy()).getApnStatus(context);
		}
	}

	private boolean isICS() {
		return (Build.VERSION.SDK_INT >= 14 ? true : false);
	}

}
